package com.bw.user.mvvm.viewmodel;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm_comom.entity.BaseEntity;
import com.bw.mvvm_comom.mvvm.BaseViewModel;
import com.bw.user.mvvm.db.DaoSession;
import com.bw.user.mvvm.db.UserEntityValues;
import com.bw.user.mvvm.model.EdtUserModel;
import com.bw.user.mvvm.model.entity.UserDaoEntity;
import com.bw.user.mvvm.model.entity.UserEntity;
import com.bw.user.mvvm.model.entity.UserEventEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class EdtUserViewModel extends BaseViewModel<EdtUserModel> {
    public ObservableField<String> head = new ObservableField<>("");
    public ObservableField<String> nick = new ObservableField<>();
    public ObservableField<Boolean> man = new ObservableField<>(true);
    public ObservableField<Boolean> woMan = new ObservableField<>(false);
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> sign = new ObservableField<>();
    UserEntityValues userEntityValues;
    private String headUrl;
    private String headName;

    @Override
    public void onCreate() {
        super.onCreate();
        List<UserEntityValues> list = new UserDaoEntity().selectAll();
        if (list == null)
            return;
        if (list.size()==0)
            return;
        userEntityValues = list.get(0);


    }

//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        EventBus.getDefault().unregister(this);
//    }


    public void selPhoto(){

    }
    public void save(){
        if (nick.get() == null){
            ToastUtils.showShort("请输入昵称");
            return;
        }
        if (phone.get() == null){
            ToastUtils.showShort("请输入手机");
            return;
        }
        if (sign.get() == null){
            ToastUtils.showShort("请输入签名");
            return;
        }
        HashMap<String,Object> map = new HashMap<>();
        if (man.get())
            map.put("gender","男");
        if (woMan.get())
            map.put("gender","女");
        map.put("id",userEntityValues.id);
        map.put("sign",sign.get());
        map.put("userIcon",headUrl);
        map.put("userName",nick.get());
        m.requestEdtUserModel(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);

    }

    public void finish(){
        getUiChange().getFinishActObServAble().setValue(""+System.currentTimeMillis());
    }

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity.getMessage().equals("修改成功")){
            UserEntity userEntity = (UserEntity) baseEntity;
            new UserDaoEntity().update(userEntity.data);
            UserEventEntity userEventEntity = new UserEventEntity();
            userEventEntity.action = "updateUser";
            EventBus.getDefault().post(userEventEntity);
            finish();
        }
    }
}
